package com.landinfo.service.impl;

import com.landinfo.domain.DataType2;
import com.landinfo.repository.DataType2Repository;
import com.landinfo.service.DataTypeService;
import com.landinfo.domain.DataType;
import com.landinfo.repository.DataTypeRepository;
import com.landinfo.service.dto.DataType2DTO;
import com.landinfo.service.dto.DataTypeDTO;
import com.landinfo.service.mapper.DataType2Mapper;
import com.landinfo.service.mapper.DataTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing DataType.
 */
@Service
@Transactional
public class DataTypeServiceImpl implements DataTypeService {

    private final Logger log = LoggerFactory.getLogger(DataTypeServiceImpl.class);

    private final DataTypeRepository dataTypeRepository;
    private final DataType2Repository dataType2Repository;


    private final DataTypeMapper dataTypeMapper;
    private final DataType2Mapper dataType2Mapper;

    @PersistenceContext
    private EntityManager entityManager;

    public DataTypeServiceImpl(DataTypeRepository dataTypeRepository, DataType2Repository dataType2Repository, DataTypeMapper dataTypeMapper, DataType2Mapper dataType2Mapper) {
        this.dataTypeRepository = dataTypeRepository;
        this.dataType2Repository = dataType2Repository;
        this.dataTypeMapper = dataTypeMapper;
        this.dataType2Mapper = dataType2Mapper;
    }

    /**
     * Save a dataType.
     *
     * @param dataTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public DataTypeDTO save(DataTypeDTO dataTypeDTO) {
        log.debug("Request to save DataType : {}", dataTypeDTO);
        DataType dataType = dataTypeMapper.toEntity(dataTypeDTO);
        dataType = dataTypeRepository.save(dataType);
        return dataTypeMapper.toDto(dataType);
    }

    /**
     * Get all the dataTypes.
     *
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<DataTypeDTO> findAll(Sort sort) {
        log.debug("Request to get all DataTypes");
        return dataTypeRepository.findAll(sort).stream()
                .map(dataTypeMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
    }

    @Override
    public List<DataType2DTO> queryByName(String name) {
        List<DataType2> dataTypes = dataType2Repository.queryByName(name);
        if (dataTypes != null && dataTypes.size() > 0) {
            return dataTypes.stream()
                    .map(dataType2Mapper::toDto)
                    .collect(Collectors.toCollection(LinkedList::new));
        } else {
            return null;
        }
    }

    /**
     * Get one dataType by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public DataType findOne(String id, Sort sort) {
        log.debug("Request to get DataType : {}", id);
//        Example example=null;
//        Girl girl = new Girl();
//        girl.setId(id);
//        Example<Girl> example = Example.of(girl);

        DataType dataType = dataTypeRepository.findById(id, sort);    //.findOne(id);
        //DataTypeDTO dataTypeDTO = dataTypeMapper.toDto(dataType);
        return dataType;
    }

    /**
     * Delete the  dataType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(String id) {
        log.debug("Request to delete DataType : {}", id);
        dataTypeRepository.deleteById(id);
    }
}
